from flask import Blueprint

from apps.foundation import db
from apps.a_common.db import safe_commit
from apps.a_common.permission import has_permission_decorate
from apps.a_common.request import assert_params_by_form
from apps.a_common.response import success_response, paginate_response
from apps.users.forms import PermissionEditorForm
from apps.users.models import Permission
from apps.users.serializers import serialize_permission

permission_blue_print = Blueprint('permission', __name__)


@permission_blue_print.route('/permission/', methods=['GET'])
@has_permission_decorate('admin-permission-view')
def _get_permissions():
    """
    获得权限列表
    ---
    tags:
      - permission
    parameters:
      - name: page_id
        in : query
        type: integer
        required: false
        default: 1
        description: 页码
      - name: page_size
        in : query
        type: integer
        required: false
        default: 20
        description: 每页的大小
    """
    queryset = Permission.query
    return paginate_response(queryset, serialize_permission)


@permission_blue_print.route('/permission/', methods=['POST'])
@has_permission_decorate('admin-permission-add')
def _post_permission():
    """
    新增权限
    ---
    tags:
      - permission
    parameters:
      - name: name
        in : formData
        type: string
        required: true
        description: 权限的名字
      - name: type
        in : formData
        type: integer
        required: true
        description: 权限的类型
    """
    form = assert_params_by_form(PermissionEditorForm)
    permission = Permission(name=form['name'], type=form['type'])
    db.session.add(permission)
    safe_commit()
    return success_response(serialize_permission(permission))


@permission_blue_print.route('/permission/<int:permission_id>/', methods=['PUT'])
@has_permission_decorate('admin-permission-change')
def _put_permission(permission_id):
    """
    修改权限
    ---
    tags:
      - permission
    parameters:
      - name: permission_id
        in : path
        type: integer
        required: true
        description: 权限的id
      - name: name
        in : formData
        type: string
        required: true
        description: 权限的名字
      - name: type
        in : formData
        type: integer
        required: true
        description: 权限的类型
    """
    form = assert_params_by_form(PermissionEditorForm)
    permission = Permission.query.filter(Permission.id == permission_id).first()
    permission.name = form['name']
    permission.type = form['type']
    safe_commit()
    return success_response(serialize_permission(permission))


@permission_blue_print.route('/permission/<int:permission_id>/', methods=['delete'])
@has_permission_decorate('admin-permission-delete')
def _delete_permission(permission_id):
    """
    删除权限
    ---
    tags:
      - permission
    parameters:
      - name: permission_id
        in : path
        type: integer
        required: true
        description: 权限的id
    """
    permission = Permission.query.filter(Permission.id == permission_id).first()
    db.session.delete(permission)
    safe_commit()
    return success_response(serialize_permission(permission))
